﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.IO;
using System.Globalization;
using System.Security.Cryptography;
using System.Net.Mail;

namespace PaydSystem.Common
{
    public class FunctionCommon
    {
        public void Logger(String lines)
        {

            string sPath = DefaultValue.logpath;
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

            DateTime dt = DateTime.Now;
            string sDate = DateTime.Now.ToString("yyyy-MM-dd");
            string sTime = DateTime.Now.ToString("HH:mm:ss");
            string sPathFile = HttpContext.Current.Request.PhysicalApplicationPath + sPath + sDate + ".txt";

            if (!File.Exists(sPathFile))
            {
                File.Create(sPathFile).Close();
            }
            StreamWriter file = new StreamWriter(sPathFile, true);
            lines = sTime + " " + lines;
            file.WriteLine(lines);

            file.Close();

        }
        public string NullToString(object Value)
        {
            return Value == null ? "" : Value.ToString();
        }
        public string EncryptData(string Message)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(DefaultValue.strSecurityKey));
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            byte[] DataToEncrypt = UTF8.GetBytes(Message);
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            return Convert.ToBase64String(Results);
        }

        public string DecryptString(string Message)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();
            MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(DefaultValue.strSecurityKey));
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;
            byte[] DataToDecrypt = Convert.FromBase64String(Message);
            try
            {
                ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }
            return UTF8.GetString(Results);
        }

        public void SendEmail(string strReceipients, string subject, string[] param ,string TemplateEmail)
        {
            var pathtemplate = HttpContext.Current.Request.PhysicalApplicationPath + TemplateEmail;
            string[] receipients = strReceipients.Split(new char[] { ',', ';' });
            //Server.MapPath(Url.Content("~/Content/Xsl/"));

            var SENDERNAME = DefaultValue.strEmailSenderName;
            var SENDER = DefaultValue.strEmailSender;
            var PWD = DefaultValue.strEmailSenderPassword;
            var SMTP = DefaultValue.strEmailSmtp;

            int PORT = 25;
            Int32.TryParse(DefaultValue.strEmailPort, out PORT);

            bool USESSL = false;
            //Boolean.TryParse(DefaultValue.strEmailUseSSL, out USESSL);




            MailMessage message = new MailMessage();
            SmtpClient smtpClient = new SmtpClient();
            string msg = string.Empty;
            try
            {
                MailAddress fromAddress = new MailAddress(SENDER, SENDERNAME);
                message.From = fromAddress;

                foreach (var r in receipients)
                    message.To.Add(r);



                string body;

                using (var sr = File.OpenText(pathtemplate))
                {
                    var template = sr.ReadToEnd();

                    body = String.Format(template, param);
                }



                message.Subject = subject;
                message.IsBodyHtml = true;
                message.Body = body;




                smtpClient.Host = SMTP;
                smtpClient.Port = PORT;
                smtpClient.EnableSsl = USESSL;
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new System.Net.NetworkCredential(SENDER, PWD);
                smtpClient.Send(message);

            }
            catch (Exception ex)
            {
                
                throw ex;
            }

        }


    }


}